package com.example.clock.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.clock.entity.AttendHistory;
import com.example.clock.entity.AttendVo;
import com.example.clock.service.AttendHistoryService;
import com.example.clock.service.AttendService;
import com.example.clock.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.LocalDateTypeHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/app")
public class AppController {

    private static final Logger logger = LoggerFactory.getLogger(AppController.class);

    @Autowired
    private AttendService attendService;

    @Autowired
    private AttendHistoryService attendHistoryService;

    //上下班打卡
    @PostMapping("/clocktodo")
    public String clock(String userName, String clockTime, Model model, HttpServletRequest request) throws UnsupportedEncodingException {
        LocalDateTime now = LocalDateTime.now();
        if (clockTime != null && !clockTime.isEmpty()) {
            if(clockTime.length()<=16){
                clockTime = clockTime+":00";
            }
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
            now = LocalDateTime.parse(clockTime, formatter);
        }
        if (userName == null || userName.isEmpty()) {
            userName = "username";
        }

        String msg = attendService.clock(userName, now);

        //添加历史记录
        attendHistoryService.saveHistory(userName, now.toLocalDate(), now, request);
        model.addAttribute("msg", msg);
        msg = URLEncoder.encode(msg, "UTF-8");
        userName = URLEncoder.encode(userName, "UTF-8");
        return "redirect:/app/getClockList?msg=" + msg + "&userName=" + userName;
    }


    @GetMapping("/getClockList")
    public String getClockList(String userName, String msg, Model model) {
        model.addAttribute("msg", msg);
        Date thisWeekMonday = DateUtil.getThisWeekMonday(new Date());
        LocalDateTime thisWeekMondayLocalDateTime = DateUtil.toLocalDateTime(thisWeekMonday);
        AttendVo data = attendService.getThisWeekClockList(userName, thisWeekMondayLocalDateTime.toLocalDate());
        LambdaQueryWrapper<AttendHistory> attendHistoryLambdaQueryWrapper = new LambdaQueryWrapper<AttendHistory>()
                .eq(AttendHistory::getCurrentDateTime, LocalDateTime.now().toLocalDate());

        List<AttendHistory> attendHistoryList = attendHistoryService.list(attendHistoryLambdaQueryWrapper);

        model.addAttribute("data", data);
        model.addAttribute("attendHistoryList", attendHistoryList);
        model.addAttribute("userName", userName);
        return "appClock";
    }
}
